package com.dashu.base.utils;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

public  final class UIDUtil {
    private static  final int SHORT_MAX=65536;
    private static int counter=-1;
    private UIDUtil(){}
    /**
     * Creates a unique 64 bits ID by aggregating the current time in
     * milliseconds since epoch (Jan. 1, 1970) and using a 16 bits counter. The
     * counter is initialized at a random number. This generator can create up
     * to 65536 different id per millisecond.
     *
     * @return a new id.
     */
    public static synchronized long getNextId() {
        long now = System.currentTimeMillis();
        if (counter == -1) {
            long seed = now ^ Thread.currentThread().getId();
            Random rnd = new Random(Long.hashCode(seed));
            counter = rnd.nextInt(SHORT_MAX);
        }
        long id = (now << 16) | counter;
        counter = (counter + 1) % SHORT_MAX;
        return id;
    }

    /**
     * generate uniq uuid
     * @return
     */
    public static synchronized String getUUID(){
        String s= UUID.randomUUID().toString();
        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
    }
    public static final long MIN_VALUE = 0x8000000000000000L;

    public static final long MAX_VALUE = 0x7fffffffffffffffL;

    final static char[] digits = { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
            'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '-', '_' };

    private static String toUnsignedString(long i, int shift) {
        char[] buf = new char[64];
        int charPos = 64;
        int radix = 1 << shift;
        long mask = radix - 1;
        do {
            buf[--charPos] = digits[(int) (i & mask)];
            i >>>= shift;
        } while (i != 0);
        return new String(buf, charPos, (64 - charPos));
    }


    // j为2的次方，如转成16进制就是4，32进制就是5...
    public static String getRand(long i,int j){
        return toUnsignedString(i, j);
    }

    // 随机码＋时间戳＋随机码的生成
    public static Long nextId(){
        String str1,str2,str3;
        str1=(new Date()).getTime()+"";
        str2=getRandStr(1);
        str3=getRandStr(1);

        //System.out.println(str1+str2+str3);
        return Long.parseLong(str1+str2+str3);
    }





    //    生成指定长度的随机串
    public static String getRandStr(Integer length){
        String str="";
        while(str.length()!=length){
            str=(Math.random()+"").substring(2,2+length);
        }
        return str;
    }

    public  static   void main(String[] args) throws Exception {
        System.out.println(nextId());
        System.out.println(String.valueOf(nextId()).length());
    }
}
